package com.hy.mydesktop.client.component.factory;

import java.util.List;

import com.extjs.gxt.ui.client.data.BaseTreeModel;
import com.extjs.gxt.ui.client.widget.menu.Menu;
import com.extjs.gxt.ui.client.widget.menu.MenuBar;
import com.extjs.gxt.ui.client.widget.menu.MenuBarItem;
import com.extjs.gxt.ui.client.widget.menu.MenuItem;
import com.hy.mydesktop.client.component.meta.AbstractComponetModelEnum;
import com.hy.mydesktop.client.component.meta.MenuBarItemModelEnum;
import com.hy.mydesktop.shared.persistence.domain.gxt.GxtComponentMetaNodeModel;

/**
 * 
 * <ul>
 * <li>开发作者：汤莉</li>
 * <li>设计日期：2010-8-30；时间：上午11:05:22</li>
 * <li>类型名称：MenuItemBarFactory</li>
 * <li>设计目的：</li>
 * </ul>
 * <ul>
 * <b>修订编号：</b>
 * <li>修订日期：</li>
 * <li>修订作者：</li>
 * <li>修订原因：</li>
 * <li>修订内容：</li>
 * </ul>
 */
public class MenuItemBarFactory {

	/**
	 * 
	 * <ul>
	 * <li>方法含义：创建菜单栏项</li>
	 * <li>方法作者：汤莉</li>
	 * <li>编写日期：2010-8-30；时间：下午上午11:06:21</li>
	 * </ul>
	 * <ul>
	 * <b>修订编号：</b>
	 * <li>修订日期：</li>
	 * <li>修订作者：</li>
	 * <li>修订原因：</li>
	 * <li>修订内容：</li>
	 * </ul>
	 * @param gxtComponentMetaModel
	 * @return
	 */
	public static MenuBarItem createMenuBarItem(GxtComponentMetaNodeModel gxtComponentMetaModel){
		return configureMenuBarItem(gxtComponentMetaModel);
	}
	
	/**
	 * 
	 * <ul>
	 * <li>方法含义：配置相关信息</li>
	 * <li>方法作者：汤莉</li>
	 * <li>编写日期：2010-8-30；时间：下午上午11:07:17</li>
	 * </ul>
	 * <ul>
	 * <b>修订编号：</b>
	 * <li>修订日期：</li>
	 * <li>修订作者：</li>
	 * <li>修订原因：</li>
	 * <li>修订内容：</li>
	 * </ul>
	 * @param gxtComponentMetaNodeModel
	 * @return
	 */
	private static MenuBarItem configureMenuBarItem(GxtComponentMetaNodeModel gxtComponentMetaNodeModel){
		MenuBarItem item=createDefaultMenuBarItem();

		item = AbstractComponetFactory.configureAbstractComponet(item, gxtComponentMetaNodeModel);

		if(gxtComponentMetaNodeModel.get(AbstractComponetModelEnum.CHILDREN.name().toLowerCase())!=null){
			//添加子控件
			Menu menu = item.getMenu();
			 List<GxtComponentMetaNodeModel> list =(List<GxtComponentMetaNodeModel>)gxtComponentMetaNodeModel.get(AbstractComponetModelEnum.CHILDREN.name().toLowerCase());
			 for(int i=0;i<list.size();i++)
				{
				 	
					MenuItem subItem=MenuItemFactory.createMenuItem(list.get(i));
					menu.add(subItem);
					//item.setMenu(menu);
					//TODO item.setMenu(subItem);
				}			
		}
		
		if(gxtComponentMetaNodeModel.get(MenuBarItemModelEnum.TEXT.name().toLowerCase())!=null){
			//设置显示文本
			item.setText((String)gxtComponentMetaNodeModel.get(MenuBarItemModelEnum.TEXT.name().toLowerCase()));
		}
		if(gxtComponentMetaNodeModel.get(MenuBarItemModelEnum.MENU.name().toLowerCase())!=null){
			//设置菜单
				List<BaseTreeModel> list=gxtComponentMetaNodeModel.get(MenuBarItemModelEnum.MENUITEM1.name().toLowerCase());
				gxtComponentMetaNodeModel.set("add", "true");
				gxtComponentMetaNodeModel.set("menuitem2", list);
				Menu menu=MenuFactory.createMenu(gxtComponentMetaNodeModel);
				item.setMenu(menu);
		}
		
		return item;
	}
	
	/**
	 * 
	 * <ul>
	 * <li>方法含义：创建默认的菜单栏项</li>
	 * <li>方法作者：汤莉</li>
	 * <li>编写日期：2010-8-31；时间：下午下午02:31:18</li>
	 * </ul>
	 * <ul>
	 * <b>修订编号：</b>
	 * <li>修订日期：</li>
	 * <li>修订作者：</li>
	 * <li>修订原因：</li>
	 * <li>修订内容：</li>
	 * </ul>
	 * @return
	 */
	private static MenuBarItem createDefaultMenuBarItem(){
		Menu menu=new Menu();
		//menu.add(new MenuItem("引入"));
		MenuBarItem item=new MenuBarItem("编辑", menu);
		return item;
	}
}
